#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e3 + 10;
int arr[N][N];
int s[N][N];
int n, m, x, y;
int main()
{
	int t;
	cin >> t;
	while (t--) {
		cin >> n >> m >> x >> y;
		memset(arr, 0, sizeof(arr));
		memset(s, 0, sizeof(s));
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				cin >> arr[i][j];
				s[i][j] = s[i][j - 1] + s[i - 1][j] + arr[i][j] - s[i - 1][j - 1];
			}
		}
		int ma = -1e5 + 20;
		for (int i = 1; i + x - 1<= n; i++) {
			for (int j = 1; j + y - 1<= m; j++) {
				int x1 = i, y1 = j;
				int x2 = i + x - 1, y2 = j + y - 1;
				int ans = s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1];
				ma = max(ans, ma);
			}
		}
		cout << ma << endl;
	}
	return 0;
}